In [39]:
import os
import pandas as pd
from pandas.tools.plotting import scatter_matrix
import matplotlib.pyplot as plt
In [40]:
path = "C:/Users/leadlab/Documents/GitHub/bcopph-mhsu-surveillance/data-public/data-use-4-python-script"
print(os.getcwd(),path)

#This function get the data in excel file
def get_dataset( data_id, path):
        # if data_is = 1: fetch location.csv
        # if data_is = 2: fetch classes.csv
        #C:\Users\leadlab\Documents\GitHub\bcopph-mhsu-surveillance\data-public\data-use-4-python-script
        filepath_location = path +"/annual-location-count-2018-03-27-locations.csv"
        filepath_classes = path +"/annual-location-count-2018-03-27-classes.csv"
        filepath_map = path+"/compressor_map.csv"
        
        if(data_id == 1):
            dataset = pd.read_csv(filepath_classes, header = 0, sep = ',',error_bad_lines=False)
        elif(data_id == 2):
            dataset = pd.read_csv(filepath_location, header = 0, sep = ',',error_bad_lines=False)
        elif(data_id ==3):
            dataset = pd.read_csv(filepath_map, header = 0, sep = ',',error_bad_lines=False)

        #print(dataset.head(30))
        dataset = dataset.dropna()
        col1 = dataset['location_class_description']
        #print(col1.unique().tolist())
        #print (col1.shape)
        col1_list = col1.unique().tolist()
        return dataset
C:\Users\leadlab C:/Users/leadlab/Documents/GitHub/bcopph-mhsu-surveillance/data-public/data-use-4-python-script
In [41]:
fecth_data = get_dataset(3,path)
print(fecth_data.head())
print (fecth_data.shape)
   location_map_id  location_class_code  \
0                1                   26   
1                2                   78   
2                3                  139   
3                4                  148   
4                5                   25   

                          location_class_description          intensity_type  \
0       Residential Care - MHSU - Rental Supplements        Residential Care   
1                                      ED - Med-Surg  ED, Urgent Care, Acute   
2                   Surgery - Procedure - Mixed Ages                 Surgery   
3                                    Medical Imaging         Medical Imaging   
4  Addictions - Post Withdrawal Stabilization (se...        Residential Care   

             intensity_severity_risk      clinical_focus  \
0                  Rental Supplement                MHSU   
1                  Emergent-Hospital  Emergency Response   
2  Surgery-Procedure-Acute Admission            Surgical   
3                    Medical Imaging     Medical Imaging   
4          Medium Intensity Res Care     MHSU-Addictions   

               service_type    service_location  \
0    Rental Supplement-MHSU           Community   
1                ED-Medical            Hospital   
2         Surgery-Procedure            Hospital   
3           Medical Imaging     Medical Imaging   
4  Res Care-MHSU-Post Detox  Community Facility   

                     population_age  \
0  Adults, some adols, older adults   
1                        Mixed Ages   
2                        Mixed Ages   
3                        Mixed Ages   
4  Adults, some adols, older adults   

                                        provider_mix  n_unique  
0  Nursing and allied health professional, physic...         1  
1                         Physician, nurse delivered         1  
2                         Physician, nurse delivered         1  
3              Medical specialist with tech supports         1  
4  Nursing and allied health professional, physic...         1  
(2231, 11)
In [46]:
#This function is cleans the file name before printing
def clean_file_b4_used(filename):
    #print (list(filename))
    special_character = ['&','/','(',')','*','%','@','^','#','!','?'] # This is the list of special character that supposed not to be included in a file name
    new_fname = ""
    for i in list(filename):
        if any(i in s for s in special_character):
            a=0
        else:
            new_fname = new_fname + i
    return new_fname

#This function is to get the list of all the class code in the dataframe
def get_class_code_list(dataset):
    location_class_code_unique = dataset['location_class_code'].unique().tolist() # This get the list of unque class code or class id
    return location_class_code_unique

def visualization(dataset, class_code_id,filepath):
    
    fig = plt.figure(figsize=(10,5))
    ax = fig.add_subplot(111)
    dataset["event_year"] = (dataset['event_year'].astype('str'))
    #print (dataset.head(10))
    
    df= (dataset.where(( dataset["location_class_code"] == int(class_code_id)))).copy()
    location_class_description = df['location_class_description'].unique().tolist()
    class_name = clean_file_b4_used((str(location_class_description[1])))
    
    title = "Trend Analytics For " + class_name + "-"+str(class_code_id)
    #print ("Unique Para Type",type(location_class_description), location_class_description, len(df.index), (location_class_code_unique))
    plt.xlabel("Time")
    plt.ylabel("Parameter Total Number")
    #plt.legend(loc='best')
    #ax.legend(loc='best')
    #plt.gca().legend(loc='upper left')
    ax.set_title(title)
    plt.xticks(rotation=90)
    df =df.dropna()
    # red dashes, blue squares and green triangles
    plt.plot(df["event_year"], df["n_people"], 'r', df["event_year"], df["n_encounters"], 'ms', df["event_year"], df["n_locations"], 'k^')
    plt.show()
    #for row in dataset.itertuples(): 
    #print(df.head(10))
    #print("Data Seize",df.shape)
    
    
    fig = plt.figure(figsize=(10,5))
    ax = fig.add_subplot(111)
    plt.subplot(3, 1, 1)
    plt.plot(df["event_year"], df["n_people"], 'r')
    plt.title(title)
    plt.ylabel('n_people')
    #plt.xticks(rotation=90)
    
    plt.subplot(3, 1, 2)
    plt.plot(df["event_year"], df["n_encounters"], 'b-')
    plt.xlabel('time (s)')
    plt.ylabel('n_encounter')
    #plt.xticks(rotation=90)
    
    plt.subplot(3, 1, 3)
    plt.plot(df["event_year"], df["n_locations"], 'k')
    plt.xlabel('time (s)')
    plt.ylabel('n_location')
    plt.xticks(rotation=90)
    plt.savefig(filepath+"/"+class_name)
    plt.show()

#Function to display the images
def display(filepath,path):
    print("*********************************************************************************")
    data_classes = get_dataset(1,path) #classes
    data_location= get_dataset(2,path) #location
    data_map = get_dataset(3,path) #maping
    #class_code = 66
    #visualization(data_location,class_code)
    print("********************************************************************************")
    class_code_list = get_class_code_list(data_classes)
    for i in class_code_list:
        visualization(data_classes,i,filepath)
    
    
In [47]:
filepath_2save_images = path + "/class_report_printout" # This is the path to save the image files generated
print (filepath_2save_images)
display(filepath_2save_images,path)
C:/Users/leadlab/Documents/GitHub/bcopph-mhsu-surveillance/data-public/data-use-4-python-script/class_report_printout
*********************************************************************************
********************************************************************************